{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 条件选择\n",
    "from SubFunc import inputInt\n",
    "\n",
    "\n",
    "currentMoney: int = inputInt(\"请输入钱: \")\n",
    "\n",
    "if 30 <= currentMoney:\n",
    "    print(\"吃肯德基\")\n",
    "    # 执行完第一个后,会直接跳过所有其他Else\n",
    "elif 20 <= currentMoney:\n",
    "    print(\"吃麻辣烫\")\n",
    "elif 10 <= currentMoney:\n",
    "    print(\"吃面包\")\n",
    "else:\n",
    "    print(\"吃其他\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "非空元组\n"
     ]
    }
   ],
   "source": [
    "# 隐式判断\n",
    "if None:\n",
    "    print(\"None\")\n",
    "\n",
    "if \"\":\n",
    "    print(\"空字符串\")\n",
    "\n",
    "if ():\n",
    "    print(\"空元组\")\n",
    "\n",
    "if (1,):  # 必须有元素才会按True识别\n",
    "    print(\"非空元组\")\n",
    "\n",
    "\n",
    "if []:\n",
    "    print(\"空数组\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 三元运算符\n",
    "\n",
    "x: str = \"吃肯德基\" if True else \"吃其他的\"\n",
    "print(x)\n",
    "\n",
    "y: str = \"吃肯德基\" if False else \"吃其他的\"\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习-用列表推导式和三元运算符去重\n",
    "\n",
    "list1: list[int] = [1, 2, 2, 3, 3, 3]\n",
    "\n",
    "o1: list[int] = [i for i in set(list1)]\n",
    "print(o1)\n",
    "\n",
    "\n",
    "o2 = list(set(list1))  # 不如这样写更简洁\n",
    "print(o2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环\n",
    "for i in range(9 + 1):  # 从0开始循环10次,一直到9\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用循环输出99乘法表\n",
    "\n",
    "for x in range(1, 9 + 1):\n",
    "    for y in range(1, 9 + 1):\n",
    "        if x >= y:\n",
    "            result1 = str(y * x)\n",
    "            result2 = result1 + (\n",
    "                \"\" if len(result1) <= 1 else \" \"\n",
    "            )  # 如果结果的长度不足2位,则补一个空格\n",
    "            print(f\"{y} * {x} = {result2}\", end=\"    \")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习输出倒着的圣诞树\n",
    "for x in range(1, 9 + 1):\n",
    "    for y in range(1, 9 + 1):\n",
    "        if x <= y:\n",
    "            print(\"*\", end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习转置矩阵\n",
    "\n",
    "list1 = [\n",
    "    [1, 2, 3],\n",
    "    [4, 5, 6],\n",
    "    [7, 8, 9],\n",
    "]\n",
    "\n",
    "list1Rows = len(list1)\n",
    "list1Cols = len(list1[0]) if list1 else 0\n",
    "\n",
    "# 初始化输出list\n",
    "list2 = [[0] * list1Rows for _ in range(list1Cols)]\n",
    "\n",
    "for r in range(list1Rows):\n",
    "    for c in range(list1Cols):\n",
    "        list2[c][r] = list1[r][c]\n",
    "\n",
    "for i in list2:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# While死循环\n",
    "while True:\n",
    "    print(\"死循环\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# While正常的循环\n",
    "i = 0\n",
    "while i <= 10:\n",
    "    print(i)\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Break和Continue的区别\n",
    "\n",
    "for i in range(5):\n",
    "    if i == 3:\n",
    "        break\n",
    "    print(f\"break:{i}\")  # 等于5的时候跳出循环\n",
    "\n",
    "for i in range(5):\n",
    "    if i == 3:\n",
    "        continue\n",
    "    print(f\"continue:{i}\")  # 等于5的时候,只跳过后续代码,但还是会回到循环开头头继续执行"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
